﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

/* ############################################################################################
 *           Matthijs Hilgers - Gerben Boot - Marcel Boelen - Joep van den Hoven
 *               1580499          1575754        1582702            1592146         
 * ############################################################################################
 * 
 *  Date     || Changed                                  || By              || Source
 *  ===========================================================================================
 *  08-03-12 || new                                      || Marcel/Joep     || new
 *  28-03-12 || from ocr method to class and change input|| Gerben          || Marcel/Joep
 */  

namespace MultipleOutputNeuralNetwork {
    class CropAlgorithm {
        public Bitmap cropImage(Bitmap b) {
            int firstline = 0;
            int lastline = 0;
            int firstcolumn = 0;
            int lastcolumn = 0;
            Boolean foundfirst = false;

            Bitmap bmp = new Bitmap(b);
            ImageData lockImage = new ImageData();
            byte[] Values = lockImage.bitmapToArray(bmp);

            //Find croplocations
            for (int i = 0; i < lockImage.imagePixels; i++) {
                long pixel = i * lockImage.pixelFormat;
                if (Values[pixel] <= 10) {
                    if (!foundfirst) {
                        firstline = i / lockImage.imageWidth;
                        firstcolumn = i % lockImage.imageWidth;
                        lastcolumn = i % lockImage.imageWidth;
                        foundfirst = true;
                    } else {
                        lastline = i / lockImage.imageWidth;
                    }
                    if (i % lockImage.imageWidth < firstcolumn) {
                        firstcolumn = i % lockImage.imageWidth;
                    } else if (i % lockImage.imageWidth > lastcolumn) {
                        lastcolumn = i % lockImage.imageWidth;
                    }
                }
            }

            //Calculate width and height for the new image (cropped)
            int newWidth = lastcolumn - firstcolumn;
            int newHeight = lastline - firstline;
            Bitmap newImage = new Bitmap(newWidth, newHeight);

            //lockimage
            ImageData newimageLock = new ImageData();
            byte[] newrgbValues = newimageLock.bitmapToArray(newImage);

            //Fill cropped image with old values
            int newpixel = 0;
            for (int j = firstline; j < lastline; j++) {
                for (int i = firstcolumn; i < lastcolumn; i++) {
                    int pixel = (j * lockImage.imageWidth + i) * lockImage.pixelFormat;
                    newrgbValues[newpixel] = Values[pixel];
                    newrgbValues[newpixel + 1] = Values[pixel + 1];
                    newrgbValues[newpixel + 2] = Values[pixel + 2];
                    newrgbValues[newpixel + 3] = Values[pixel + 3];
                    newpixel += lockImage.pixelFormat;
                }
            }

            //copy new values into the image
            newImage = lockImage.arrayToBitmap(newImage, newrgbValues);

            return newImage;
        }
    }
}
